package com.beiben.myjob.service.impl;

import com.beiben.myjob.domain.CheckPeopleVo;
import com.beiben.myjob.domain.CheckUserVo;
import com.beiben.myjob.service.MyWaitWorkService;
import com.beiben.utils.DateUtil;
import com.beiben.work.process.domain.FileProcessUser;
import com.beiben.work.process.mapper.FileProcessUserMapper;
import com.beiben.workflow.domain.WfApplyVo;
import com.beiben.workflow.domain.WfCheck;
import com.beiben.workflow.service.IWfApplyService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 我的待办工作
 */
@Service
public class MyWaitWorkServiceImpl implements MyWaitWorkService {

    @Autowired
    private FileProcessUserMapper fileProcessUserMapper;
    @Autowired
    private IWfApplyService wfApplyService;

    /**
     * 查询我的待办工作-日常工作
     */
    @Override
    @DataScope(userAlias = "u")
    public List<FileProcessUser> selectMyWaitWorkList(FileProcessUser fileProcessUser) {
        List<FileProcessUser> list = fileProcessUserMapper.selectMyWaitWorkList(fileProcessUser);
        isOutTime(list);
        return list;
    }

    /**
     * 查询我的待办工作-项目工作
     *
     * @param fileProcessUser
     * @return
     */
    @Override
    @DataScope(userAlias = "u")
    public List<FileProcessUser> selectMyWaitWorkProjectList(FileProcessUser fileProcessUser) {
        List<FileProcessUser> list = fileProcessUserMapper.selectMyWaitWorkProjectList(fileProcessUser);
        isOutTime(list);
        return list;
    }

    /**
     * 我的待办工作统计
     *
     * @return
     */
    @Override
    @DataScope(userAlias = "u")
    public Map<String, Object> statistics(SysUser user) {
        Map<String, Object> result = fileProcessUserMapper.statistics(user);
        return result;
    }

    /**
     * 提交审核（启用事务管理）
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = 72000, rollbackFor = Exception.class)
    public Long submitCheck(CheckUserVo checkUserVo) {
        FileProcessUser fileProcessUser = fileProcessUserMapper.selectFileProcessUserById(checkUserVo.getTableId());
        // TODO: 2020/12/18 转换为我的申请
        WfApplyVo wfApply = new WfApplyVo();
        // 申请人id
        wfApply.setApplyUserId(fileProcessUser.getUserId());
        // 待办工作id 对应 work_file_process_user 、 pm_project
        Long tableId = checkUserVo.getTableId();
        wfApply.setTableId(tableId);
        // table 对应类型
        wfApply.setApplyType(checkUserVo.getApplyType());
        // 审批内容 借用备注字段
        wfApply.setRemark(checkUserVo.getContent());
        // 完成情况 2021-01-19 新增字段 提交申请时 提交人 必填
        wfApply.setCompletion(checkUserVo.getCompletion());
        //人员列表（申请人+审核人（2020-12-24：前端未传申请人））数组形式
        List<WfCheck> checks = new ArrayList<>();
        for (CheckPeopleVo cv : checkUserVo.getCheckList()) {
            String type = cv.getType();
            //过滤申请人
            if ("0".equals(type)) {
                continue;
            }
            // TODO: 2020/12/18
            WfCheck wfCheck = new WfCheck();
            wfCheck.setCheckUserId(cv.getId());
            checks.add(wfCheck);
        }
        wfApply.setChecks(checks);
        //保存申请 并 保存审批流程 返回审批id
        return wfApplyService.submitApplyCheck(wfApply);
    }


    /**
     * 是否超时
     *
     * @param list
     */
    public void isOutTime(List<FileProcessUser> list) {
        for (FileProcessUser fileProcessUser : list) {
            try {
                if ("Y".equals(fileProcessUser.getIsTimeOut())) {
                    continue;
                }
                // TODO: 2021-01-19   查看超时是否存在问题
                Long poorDate = DateUtil.getDateMinus(fileProcessUser.getFinishTime(), DateUtils.parseDate(DateUtils.getDate(), DateUtils.YYYY_MM_DD));
                if (poorDate < 0) {
                    fileProcessUser.setIsTimeOut("Y");
                    fileProcessUserMapper.updateFileProcessUser(fileProcessUser);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
}
